package cn.tedu.submarine;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.util.Arrays;
import java.awt.Graphics;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 游戏窗口
 */
public class World extends JPanel{
    public static final int WIDTH=641 ;
    public static final int HEIGHT =479 ;
    //窗口中能看到的对象
    private Battleship ship=new Battleship();
    private SeaObject[] submarines={};//潜艇数组
    private Mine[] mines={};//水雷数组
    private Bomb[] bombs={};//炸弹数组

    /**
     * 生成潜艇数组
     * @return
     */
    private SeaObject nextSubmarine(){
        Random rand=new Random();
        int type=rand.nextInt(20);
        if (type<10){//返回侦察潜艇
            return new ObserveSubmarine();
        }else if(type<16){//鱼雷
            return new TorpedoSubmarine();
        }else {//水雷
            return new MineSubmarine();
        }
    }

    //潜艇入场计数
    private int subEnterIndex=0;
    private void submarineEnterAction(){
        subEnterIndex++;
        if(subEnterIndex%10==0){
            SeaObject obj=nextSubmarine();
            submarines= Arrays.copyOf(submarines,submarines.length+1);//扩容
            submarines[submarines.length-1]=obj;
        }
    }
    //水雷入场技术
    private int mineEnterIndex=0;
    private void mineEnterAction(){
        mineEnterIndex++;
        if(mineEnterIndex%10==0){
            for (int i = 0; i < submarines.length; i++) {
                if(submarines[i] instanceof MineSubmarine){
                    MineSubmarine ms=(MineSubmarine) submarines[i];
                    Mine obj=ms.shootMine();
                    mines=Arrays.copyOf(mines,mines.length+1);
                    mines[mines.length-1]=obj;
                }
            }
        }
    }

    /**
     * haiyang对象移动
     */
    private void moveAction(){
        for (int i = 0; i < submarines.length; i++) {
            submarines[i].move();
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].move();
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].move();
        }
    }

    /**
     * 启动程序执行
     */
    public void action(){
        Timer timer=new Timer();
        int interval=10;
        /*timer.schedule(()->{
            submarineEnterAction();
            moveAction();
            mineEnterAction();
            repaint();
        },interval,interval);*/
       timer.schedule(new TimerTask() {
            @Override
            public void run() {
                submarineEnterAction();
                mineEnterAction();
                moveAction();
                repaint();
            }
        }, interval, interval);

    }

    /**
     * 重写【
     * @param g
     */
    public void paint(Graphics g) {
        Images.sea.paintIcon(null,g,0,0);
        ship.paintImage(g);
        for (int i = 0; i <submarines.length ; i++) {
            submarines[i].paintImage(g);
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].paintImage(g);
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].paintImage(g);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame(); //3.
        World world = new World(); //会创建窗口中的那一堆对象
        world.setFocusable(true);
        frame.add(world);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH+16, HEIGHT+39);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true); //系统自动调用paint()方法

        world.action(); //启动程序的执行
    }

}
